package com.yswl.yeeduo.ui.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.yswl.yeeduo.App;
import com.yswl.yeeduo.R;
import com.yswl.yeeduo.tool.KSystemTool;
import com.yswl.yeeduo.ui.base.BaseActivity;
import com.yswl.yeeduo.ui.task.WXAuthByUm;
import com.yswl.yeeduo.ui.tool.DataCleanManager;
import com.yswl.yeeduo.ui.tool.ServiceAuthManager;
import com.yswl.yeeduo.ui.tool.UserManger;
import com.yswl.yeeduo.view.dialog.ComfireAlertDialog;
import com.yswl.yeeduo.view.dialog.ComfireAlertDialog.ComfireAlertDialogListner;

/**
 * 系统设置界面
 * 
 * @author Administrator
 *
 */
public class SettingActivity extends BaseActivity implements OnClickListener, ComfireAlertDialogListner {

	LinearLayout layout;
	TextView cacheTv;
	String mCahce;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		setHeader("系统设置");

		initUI();
	}

	private void initUI() {
		Button loginOff = (Button) findViewById(R.id.setting_log_off);
		Button foxPwd = (Button) findViewById(R.id.btn_foxpwd);
		loginOff.setOnClickListener(this);
		foxPwd.setOnClickListener(this);

		try {
			mCahce = DataCleanManager.getTotalCacheSize(this);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		cacheTv = (TextView) findViewById(R.id.cache_tv);
		cacheTv.setText(mCahce);
		RelativeLayout clearCache = (RelativeLayout) findViewById(R.id.clear_cache_btn);
		clearCache.setOnClickListener(this);

		TextView version = (TextView) findViewById(R.id.version_tv);
		version.setText("V " + KSystemTool.getAppVersionName(this));
	}

	// Um
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(this.getClass().getSimpleName());
		MobclickAgent.onResume(this); // 统计时长
		layout = (LinearLayout) findViewById(R.id.layout);
		if (!App.getInstance().isLogin) {
			layout.setVisibility(View.GONE);
		} else {
			layout.setVisibility(View.VISIBLE);
		}
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(this.getClass().getSimpleName());
		MobclickAgent.onPause(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.clear_cache_btn:
			ComfireAlertDialog.getComfireAlertDialog(this, this, 001, null, "当前缓存大小为" + mCahce + ",确定要清理缓存吗？", "取消", "确定").show();
			break;
		case R.id.btn_foxpwd:
			startActivity(new Intent(this, FoxPwdActivity.class));
			break;
		case R.id.setting_log_off:
			ComfireAlertDialog.getComfireAlertDialog(this, this, 002, null, "确定要退出登录吗？", "取消", "确定").show();
			break;

		default:
			break;
		}

	}

	@Override
	public void ComfireAlertDialogSure(int targetId, AlertDialog dialog, Object param) {
		switch (targetId) {
		case 002:// 退出登录
			// new WXAuthByUm(this).exitWxLogin();//撤销微信授权
			MobclickAgent.onEvent(this, "user_login_out");
			App.getInstance().isLogin = false;
			ServiceAuthManager.saveAuthParams(this, "", "", "");
			if (UserManger.getInstance().isAuthed(this)) {
//				UserManger.getInstance().recordWxParameterOfOpenId(this, "");// 每次登陆清空微信授权在用户换一个注册号码时提现不了
//				new WXAuthByUm(this).exitWxLogin();		//微信注销	
			}
			startActivity(new Intent(this, LoginActivity.class));
			finish();
			break;
		case 001:// 清空
			MobclickAgent.onEvent(this, "user_clear_cache");
			DataCleanManager.clearAllCache(this);
			try {
				mCahce = DataCleanManager.getTotalCacheSize(this);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			cacheTv.setText(mCahce);
			break;
		default:
			break;
		}
		dialog.cancel();

	}

	@Override
	public void ComfireAlertDialogCancel(int targetId, AlertDialog dialog) {
		dialog.cancel();
	}
}
